package com.team.book.service.impl;

import com.team.book.common.constant.BooleanEnum;
import com.team.book.common.constant.ResponseStatus;
import com.team.book.common.domain.Page;
import com.team.book.common.exception.UncheckedException;
import com.team.book.common.util.DateUtil;
import com.team.book.common.util.UserUtil;
import com.team.book.domain.BorrowedRecord;
import com.team.book.domain.BorrowedRecordExample;
import com.team.book.domain.User;
import com.team.book.mapper.BorrowedRecordMapper;
import com.team.book.service.BorrowedRecordService;
import com.team.book.web.dto.BorrowedRecordDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author Joann
 * @date 2017年5月6日 下午11:38:55
 */
@Service
public class BorrowedRecordServiceImpl implements BorrowedRecordService {
	@Autowired
	public BorrowedRecordMapper borrowedRecordMapper;

	@Override
	public Page<BorrowedRecord> query(Page<BorrowedRecord> page, BorrowedRecordDTO record) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("record",record);
		long count = borrowedRecordMapper.count(map);
		map.put("limit",page.getPageSize());
		map.put("offset",page.getOffset());
		page.setTotalCount(count);
		List<BorrowedRecord> list = borrowedRecordMapper.query(map);
		page.setResult(list);
		return page;
	}

	/**
	 * 借书记录
	 * @param code
	 * @return
     */
	@Override
	public int save(long code) {
		BorrowedRecord record = new BorrowedRecord();
		record.setBookcode(code);
		record.setCreatetime(DateUtil.curDate());
		record.setStatus(BooleanEnum.TRUE.getCode());
		record.setBorrowStatus(BooleanEnum.FALSE.getCode());
		record.setStarttime(record.getCreatetime());
		User user = UserUtil.getCurUser();
		record.setBorrower(user.getId());
		record.setCreateuser(user.getId());
		return borrowedRecordMapper.insert(record);
	}

	/**
	 * 还书记录
	 * @param code
	 * @return
	 * @throws UncheckedException
     */
	@Override
	public int returnBook(long code) throws UncheckedException {
		BorrowedRecordExample ex = new BorrowedRecordExample();
		ex.createCriteria().andBookcodeEqualTo(code).andBorrowStatusEqualTo(BooleanEnum.FALSE.getCode());
		List<BorrowedRecord> recordArray = borrowedRecordMapper.selectByExample(ex);
		if(recordArray==null||recordArray.size()<=0){
			throw  new UncheckedException(ResponseStatus.NOT_FOUND);
		}
		BorrowedRecord record = recordArray.get(0);
		record.setBorrowStatus(BooleanEnum.TRUE.getCode());
		record.setEndtime(DateUtil.curDate());
		User user = UserUtil.getCurUser();
		record.setUpdateuser(user.getId());
		return borrowedRecordMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int del(long id) {
		BorrowedRecord borrowedRecord = new BorrowedRecord();
		borrowedRecord.setId(id);
		borrowedRecord.setStatus(BooleanEnum.FALSE.getCode());
		return borrowedRecordMapper.updateByPrimaryKeySelective(borrowedRecord);
	}
}
